package cn.scw.framework.protocol.tcp;

import cn.scw.common.utils.ByteUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 数据包
 */
public class ProtoBytePack extends ByteToMessageDecoder {

    private static Logger logger= LoggerFactory.getLogger(ProtoBytePack.class);

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        try {

            // 读取data数据
            byte[] data = new byte[byteBuf.readableBytes()];
            byteBuf.readBytes(data);
            //放入协议包处理hander
            list.add(data);

        } catch (Exception ex) {
            //打印日志
            logger.info("tcp解析异常", ex);
            if (null != byteBuf) {
                logger.info("[content:" + ByteUtil.bytesToBinaryString(byteBuf.array()) + "]");
            }
            //释放资源
            byteBuf.release();

        }
    }
}
